#!/bin/bash
set -e
cd "$(dirname $0)"

[ "$#" -eq 1 ] || die "One Account ID argument required, $# provided"

export ACCOUNT_ID=$1
ONE_YOCTO=0.000000000000000000000001

export SKYWARD_TOKEN_ID=token.$ACCOUNT_ID
export CONTRACT_ID=$ACCOUNT_ID

near create-account $SKYWARD_TOKEN_ID --masterAccount=$ACCOUNT_ID --initialBalance=3
near deploy $SKYWARD_TOKEN_ID common/fungible_token.wasm new '{"owner_id": "'$ACCOUNT_ID'", "total_supply": "1000000000000000000000000", "metadata": {"spec": "ft-1.0.0", "name": "Skyward Finance Token", "symbol": "SKYWARD", "icon": "", "decimals": 18}}'

export WRAP_NEAR_TOKEN_ID=wrap.near

declare -a LOCKUP_BALANCES=("5314188080644000000000" "25312260092366000000000" "17491819666921000000000" "51881732160069000000000")

# use for loop to read all values and indexes
for (( i=0; i<4; i++ ));
do
  LOCKUP_BALANCE=${LOCKUP_BALANCES[$i]}
  LOCKUP_ACCOUNT_ID=lockup$i.$ACCOUNT_ID
  echo "Lockup ${LOCKUP_ACCOUNT_ID} with balance ${LOCKUP_BALANCE}"
  near create-account $LOCKUP_ACCOUNT_ID --masterAccount=$ACCOUNT_ID --initialBalance=20
  near deploy $LOCKUP_ACCOUNT_ID release/lockup$i.wasm new '{
    "token_account_id": "'$SKYWARD_TOKEN_ID'",
    "skyward_account_id": "'$CONTRACT_ID'",
    "claim_expiration_timestamp": 1656633600
  }' --initGas=200000000000000
  near call $SKYWARD_TOKEN_ID --accountId=$ACCOUNT_ID storage_deposit '{"account_id": "'$LOCKUP_ACCOUNT_ID'"}' --amount=0.00125
  near call $WRAP_NEAR_TOKEN_ID --accountId=$ACCOUNT_ID storage_deposit '{"account_id": "'$LOCKUP_ACCOUNT_ID'"}' --amount=0.00125
  near call $SKYWARD_TOKEN_ID --accountId=$ACCOUNT_ID ft_transfer '{"receiver_id": "'$LOCKUP_ACCOUNT_ID'", "amount": "'$LOCKUP_BALANCE'"}' --amount=$ONE_YOCTO

  near view $LOCKUP_ACCOUNT_ID get_stats
done

# 2021-08-01 = 1627776000
# 2021-09-01 = 1630454400
# 2022-01-01 = 1640995200
# 2022-07-01 = 1656633600

near deploy $CONTRACT_ID release/skyward.wasm new '{
  "skyward_token_id": "'$SKYWARD_TOKEN_ID'",
  "skyward_vesting_schedule": [{
    "start_timestamp": 1627776000,
    "end_timestamp": 1630454400,
    "amount": "10000000000000000000000"
  }, {
    "start_timestamp": 1640995200,
    "end_timestamp": 1656633600,
    "amount": "90000000000000000000000"
  }],
  "listing_fee_near": "10000000000000000000000000",
  "w_near_token_id": "'$WRAP_NEAR_TOKEN_ID'"
}'

near call $SKYWARD_TOKEN_ID --accountId=$ACCOUNT_ID storage_deposit '{"account_id": "'$CONTRACT_ID'"}' --amount=0.00125
near call $WRAP_NEAR_TOKEN_ID --accountId=$ACCOUNT_ID storage_deposit '{"account_id": "'$CONTRACT_ID'"}' --amount=0.00125
